//
//  RTStackView.swift
//  Weibo
//
//  Created by Brisinga on 16/3/13.
//  Copyright © 2016年 Root. All rights reserved.
//

import UIKit

enum RTComposeToolBarButtonType: Int {
    
    case Picture = 0, Mention, Trend, Emoticon, Add
}

class RTComposeToolBar: UIStackView {
    
    
    // 判断是否是系统键盘
    var isSystemKeyboard: Bool = true {
        
        didSet {
            
            let btn = self.viewWithTag(RTComposeToolBarButtonType.Emoticon.rawValue) as! UIButton
            
            var imageName = "compose_keyboardbutton_background"
            
            if isSystemKeyboard {
                
                imageName = "compose_emoticonbutton_background"
            }
            
            btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
            
            btn.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        }
    }
    
    
    // composeToolBarButton点击事件
    var composeToolBarButtonCallBack:((type: RTComposeToolBarButtonType)->())?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        // 加载控件
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 加载控件
    private func setupUI() {
    
        distribution = .FillEqually
        
        addChildButton("compose_toolbar_picture", type: .Picture)
        addChildButton("compose_mentionbutton_background", type: .Mention)
        addChildButton("compose_trendbutton_background", type: .Trend)
        addChildButton("compose_emoticonbutton_background", type: .Emoticon)
        addChildButton("compose_add_background", type: .Add)
        
    }
    
    
    // 添加按钮
    private func addChildButton(imageName: String,type: RTComposeToolBarButtonType) {
    
        let button = UIButton()
        
        button.addTarget(self, action: "composeToolBarButtonDidClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        
        button.tag = type.rawValue
        
        addArrangedSubview(button)
    }
    
    
    @objc private func composeToolBarButtonDidClick(button: UIButton) {
    
        composeToolBarButtonCallBack?(type:RTComposeToolBarButtonType(rawValue: button.tag)!)
    }

}


